package com;

import java.sql.*;

public class JDBCUPDATE {
    public static void main(String[] args) throws Exception {
        //加载驱动器
        Class.forName("com.mysql.cj.jdbc.Driver");
        //获取连接对象
        Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/day02","root","root");
        String sql ="insert into teacher(name,age,sex,sizhi) value(?,?,?,?)";
        PreparedStatement ps = conn.prepareStatement(sql);
        ps.setString(1,"小老虎");
        ps.setInt(2,600);
        ps.setString(3,"男");
        ps.setString(4,"智利");
        //执行sql语句
        ps.executeUpdate();

        //查询一遍
        String sqlSelect  = "select * from teacher";
        Statement statement=conn.createStatement();
        ResultSet resultset = statement.executeQuery(sqlSelect);
        //获得结果集
        while (resultset.next()){
            System.out.println(resultset.getString("name")+"    "+resultset.getString("age")+"    "+resultset.getString("sex")+"    "+resultset.getString("sizhi"));
        }
        ps.close();
        conn.close();
        statement.close();
    }
}
class JDBCUPDATE1 {
    public static void main(String[] args) throws Exception {
        //加载驱动器
        Class.forName("com.mysql.cj.jdbc.Driver");
        //获取连接对象
        Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/day02","root","root");
        String sqlAdd ="update teacher set age=?,sizhi=? where name = ?";
        PreparedStatement ps = conn.prepareStatement(sqlAdd);
        ps.setString(1,"900");
        ps.setString(2,"韩国");
        ps.setString(3,"王刚");
        //执行sql语句
        ps.executeUpdate();

        //查询一遍
        String sqlSelect  = "select * from teacher";
        Statement statement=conn.createStatement();
        ResultSet resultset = statement.executeQuery(sqlSelect);
        //获得结果集
        while (resultset.next()){
            System.out.println(resultset.getString("name")+"    "+resultset.getString("age")+"    "+resultset.getString("sex")+"    "+resultset.getString("sizhi"));
        }
        ps.close();
        conn.close();
        statement.close();
    }
}
class JDBCUPDATE2 {
    public static void main(String[] args) throws Exception {
        //加载驱动器
        Class.forName("com.mysql.cj.jdbc.Driver");
        //获取连接对象
        Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/day02","root","root");
        String sqlAdd ="delete from teacher where name = ?";
        PreparedStatement ps = conn.prepareStatement(sqlAdd);
        ps.setString(1,"李四");
        //执行sql语句
        ps.executeUpdate();

        //查询一遍
        String sqlSelect  = "select * from teacher";
        Statement statement=conn.createStatement();
        ResultSet resultset = statement.executeQuery(sqlSelect);
        //获得结果集
        while (resultset.next()){
            System.out.println(resultset.getString("name")+"    "+resultset.getString("age")+"    "+resultset.getString("sex")+"    "+resultset.getString("sizhi"));
        }
        ps.close();
        conn.close();
        statement.close();
    }
}